<?php
function gen_sitemap($sql,$date,$cycle,$name)
{	
	global $DH_output_path,$DH_home_url,$DH_html_url;
	
	$timetmp = strtotime($date);
	$updatetime = date("Y-m-d",$timetmp)."T".date("H:i:s",$timetmp)."+00:00";

	
	$DH_input_html  = $DH_output_path . 'gen/sitemap/sitemap/sitemap.xml';
	$DH_sitemap = dh_file_get_contents("$DH_input_html");
	$DH_sitemap = str_replace("%lastdate%",$updatetime,$DH_sitemap);
	$DH_input_html  = $DH_output_path . 'gen/sitemap/sitemap/sitemap_each.xml';
	$DH_sitemap_each = dh_file_get_contents("$DH_input_html");	
	
	$DH_input_html  = $DH_output_path . 'gen/sitemap/sitemap/sitemap_baidu.xml';
	$DH_sitemap_baidu = dh_file_get_contents("$DH_input_html");	
	$DH_sitemap_baidu = str_replace("%lastdate%",$updatetime,$DH_sitemap_baidu);
	$DH_input_html  = $DH_output_path . 'gen/sitemap/sitemap/sitemap_baidu_each.xml';
	$DH_sitemap_baidu_each = dh_file_get_contents("$DH_input_html");		
	
	$rs = mysql_query($sql);
	$sitemap_all='';
	$sitemap_baidu_all='';
	
	//如果是每周更新，那么是第一个页面，就需要添加首页的链接
	if($cycle==='weekly')
	{
		$sitemap_each = str_replace("%url%",$DH_home_url,$DH_sitemap_each);
		$sitemap_each = str_replace("%updatetime%",$updatetime,$sitemap_each);
		$sitemap_each = str_replace("%cycle%",'daily',$sitemap_each);
		$sitemap_each = str_replace("%priority%",'1.0',$sitemap_each);
		$sitemap_all.=$sitemap_each;
		
		$sitemap_baidu_each = str_replace("%url%",$DH_home_url,$DH_sitemap_baidu_each);		
		$sitemap_baidu_each = str_replace("%updatetime%",$updatetime,$sitemap_baidu_each);	
		$title = '二手电影-影视资源,搜集分享';	
		$sitemap_baidu_each = str_replace("%title%",$title,$sitemap_baidu_each);		
		$sitemap_baidu_all.=$sitemap_baidu_each;		
	}
	
	while($row = mysql_fetch_array($rs))
	{
		$htmlpath = output_page_path($DH_html_url,$row['id']);
		$sitemap_each = str_replace("%url%",$htmlpath,$DH_sitemap_each);
		$timetmp = strtotime($row['updatetime']);
		$updatetime = date("Y-m-d",$timetmp)."T".date("H:i:s",$timetmp)."+00:00";
		
		$sitemap_each = str_replace("%updatetime%",$updatetime,$sitemap_each);
		$sitemap_each = str_replace("%cycle%",$cycle,$sitemap_each);
		$sitemap_each = str_replace("%priority%",'0.2',$sitemap_each);
		$sitemap_all.=$sitemap_each;
				
		$sitemap_baidu_each = str_replace("%url%",$htmlpath,$DH_sitemap_baidu_each);
		$sitemap_baidu_each = str_replace("%updatetime%",$row['updatetime'],$sitemap_baidu_each);	
		$title = $row['title'];
		if($row['aka']!='')
		{
			$akas=preg_split("/[\/]+/", $row['aka']);
			$title.=' '.$akas[0];
			//echo $title."</br>\n";
		}
		$title .=' 在线下载资源(二手电影)';
		$sitemap_baidu_each = str_replace("%title%",$title,$sitemap_baidu_each);		
		$sitemap_baidu_all.=$sitemap_baidu_each;
	}
	$DH_sitemap = str_replace("%sitemaps%",$sitemap_all,$DH_sitemap);
	$DH_sitemap_baidu = str_replace("%sitemaps%",$sitemap_baidu_all,$DH_sitemap_baidu);
	
	$DH_output_file = $DH_output_path.'sitemap'.$name.'.xml';
	dh_file_put_contents($DH_output_file,$DH_sitemap);	
	$DH_output_file = $DH_output_path.'sitemap_baidu'.$name.'.xml';	
	dh_file_put_contents($DH_output_file,$DH_sitemap_baidu);		
}

function genhtml($sql,$i,$times)
{
	global $movietype,$moviecountry,$DH_html_url,$DH_home_url,$DH_output_path;
	$DH_input_html  = $DH_output_path . 'gen/sitemap/sitemap/sitemap.html';
	$DH_output_content = dh_file_get_contents("$DH_input_html");
	$DH_output_content = str_replace("%home%",$DH_home_url,$DH_output_content);
	$results=dh_mysql_query($sql);
	if($results)
	{
		$liout='';
		while($row = mysql_fetch_array($results))
		{
			//类别
			$type="";
			preg_match('/<t>(.*?)<\/t>/',$row['meta'],$match);
			if(!empty($match[1]))
			{	
				$type = $match[1];
			}
			$htmlpath = output_page_path($DH_html_url,$row['id']);
			$updatetime=date('Y-m-d',strtotime($row['updatetime']));
			$liout.='<li> '.$row['id'].' ['.$updatetime.']['.$movietype[$row['cattype']].']['.$moviecountry[$row['catcountry']].':'.$type.'] <a href="'.$htmlpath.'" target="_blank">'.$row['title']."</a></li>\n";
		}
		$sitemaphtml = str_replace("%num%",$i,$DH_output_content);
		$sitemaphtml = str_replace("%list%",$liout,$sitemaphtml);
		
		$pagenavi = dh_pagenavi(9,$times,$DH_home_url.'sitemap',$i);			
		$sitemaphtml= str_replace("%pagenavi%",$pagenavi,$sitemaphtml);
		
		
		$DH_output_file = $DH_output_path.'sitemap'.$i.'.html';
		dh_file_put_contents($DH_output_file,$sitemaphtml);	
	}	
}

function gen_siteindex($date)
{
	global $DH_output_path,$DH_home_url,$DH_home_url;
	
	$timetmp = strtotime($date);
	$date = date("Y-m-d",$timetmp)."T".date("H:i:s",$timetmp)."+00:00";	
	
	$DH_input_html  = $DH_output_path . 'gen/sitemap/sitemap/siteindex.xml';
	$DH_siteindex = dh_file_get_contents("$DH_input_html");
	$DH_input_html  = $DH_output_path . 'gen/sitemap/sitemap/siteindex_each.xml';
	$DH_siteindex_each = dh_file_get_contents("$DH_input_html");	
	$DH_siteindex_each = str_replace("%home%",$DH_home_url,$DH_siteindex_each);
	
	$DH_input_html  = $DH_output_path . 'gen/sitemap/sitemap/robots.txt';
	$DH_robots = dh_file_get_contents("$DH_input_html");
		
	$siteindex_all='';
	$siteindex_baidu_all='';
	$siterobots='';
	// 用 opendir() 打开目录，失败则中止程序
	$handle = @opendir($DH_output_path) or die("Cannot open " . $dir);
	echo "<b>Files in " . $DH_output_path . ":</b><br/>\n";
	// 用 readdir 读出文件列表
	while($file = readdir($handle))
	{
		$ext=strrchr($file,'.');
		if($ext!='.xml')
			continue;
		//得到sitemap的时间	
		preg_match("/([0-9]{8})/",$file,$match);
		//print_r($match);
		
		// 选出是哪个类型的sitemap
		if(strstr($file,'sitemap_baidu'))
		{
			echo "sitemap_baidu:$file<br/>";
			$siteindex_baidu_each = str_replace("%sitemap%",$file,$DH_siteindex_each);
			if(!empty($match[1]))
			{
				$timetmp = strtotime($match[1]);
				$updatetime = date("Y-m-d",$timetmp)."T".date("H:i:s",$timetmp)."+00:00";
				$siteindex_baidu_each = str_replace("%date%",$updatetime,$siteindex_baidu_each);
			}
			else
			{
				$siteindex_baidu_each = str_replace("%date%",$date,$siteindex_baidu_each);
			}
			$siteindex_baidu_all .=$siteindex_baidu_each;
		}
		if(strstr($file,'sitemap')||(strstr($file,'sitemap_baidu')))
		{
			echo "sitemap:$file<br/>";
			$siteindex_each = str_replace("%sitemap%",$file,$DH_siteindex_each);
			if(!empty($match[1]))
			{
				$timetmp = strtotime($match[1]);
				$updatetime = date("Y-m-d",$timetmp)."T".date("H:i:s",$timetmp)."+00:00";
				$siteindex_each = str_replace("%date%",$updatetime,$siteindex_each);
			}
			else
			{
				$siteindex_each = str_replace("%date%",$date,$siteindex_each);
			}			
			$siteindex_all .=$siteindex_each;
			$siterobots	.="\nSitemap: ".$DH_home_url.$file;	
		}	
	}
	
	// 关闭目录读取
	closedir($handle);		
	
	$o_siteindex = str_replace("%sitemaps%",$siteindex_all,$DH_siteindex);
	$o_siteindex_baidu = str_replace("%sitemaps%",$siteindex_baidu_all,$DH_siteindex);
	$o_siterobots = str_replace("%sitemaps%",$siterobots,$DH_robots);
	
	$DH_output_file = $DH_output_path.'siteindex.xml';
	dh_file_put_contents($DH_output_file,$o_siteindex);	
	$DH_output_file = $DH_output_path.'siteindex_baidu.xml';	
	dh_file_put_contents($DH_output_file,$o_siteindex_baidu);
	$DH_output_file = $DH_output_path.'robots.txt';	
	dh_file_put_contents($DH_output_file,$o_siterobots);	
}
?>
